Skip to content

Build client/hello from views and hold server/activate's lists as sets - #227

Merged
kahrendt merged 3 commits into
alloc-2-base64from
alloc-3-messages
Nov 3, 2026
Merged

kahrendt merged 3 commits into
alloc-2-base64from
alloc-3-messages

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Layer 3 of 7 (base: alloc-2-base64). Accept to admission: 44 -> 24 allocations under libc++, 50 -> 27 under libstdc++.

Summary

client/hello copied its fields out of the config, and server/activate parsed its lists into vectors that the connection and the manager copied again. Both now hold views or bit sets.

Changes

  • ClientHelloMessage views the config: string_views and spans, with its roles and pair methods in InlineVectors. It is filled in place by build_client_hello(ClientHelloMessage&) (a private member in client.h) and serialized before anything it views can change.
  • platform_get_interface_mac() returns the MAC as a fixed array (MacAddressText), which the message holds when the MAC is detected rather than configured.
  • ActivitySet holds server/activate's activities as bits. ActiveRoles holds its roles as the mask the role gates already read, plus whether any role was named, so a role version this library does not implement still makes the set non-empty, as admission requires. role_from_string() replaces role_in() and active_role_mask().

Testing

  • The two-role active_roles test becomes a table: implemented roles, a version this library does not implement (no bit, but not empty), an explicit empty set, and a list with no string element.
  • The detected-MAC path runs in host tests whenever no MAC is configured, but no test asserts the MAC it sends.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the connection-setup allocation optimizations.

No open issues 🎉

What changed in this PR

This PR reduces allocations during Sendspin connection setup by building client/hello from borrowed configuration data and storing server/activate activities and roles as compact sets.

Changes:

  • Replaces hello-field copies with string views, spans, inline lists, and fixed-size detected-MAC storage.
  • Uses activity bits and role masks while preserving sticky roles and non-empty unknown-role handling.
  • Updates tests and lowers admission allocation expectations to 24 under libc++ and 27 under libstdc++.
File Description
tests/​test_source_role.cpp Adapts hello assertions and source activation setup.
tests/​test_protocol.cpp Updates view-backed fixtures and adds active-role parsing cases.
tests/​test_player_role.cpp Uses compact roles in player activation setup.
tests/​test_pairing_state_machine.cpp Adapts pairing fixtures to sets and in-place hello construction.
tests/​test_noise_transport.cpp Updates activation setup for transport failure tests.
tests/​test_network_info.cpp Validates fixed-array MAC text.
tests/​test_encrypted_lifecycle.cpp Converts lifecycle fixtures to activity and role sets.
tests/​test_dynamic_pairing_code.cpp Keeps backing lists alive for descriptor views.
tests/​test_client_lifecycle.cpp Updates lifecycle activation fixtures.
tests/​test_allocations.cpp Lowers admission allocation budgets and updates their explanation.
tests/​test_admission.cpp Uses activity sets in admission tests.
src/​protocol.cpp Parses activation sets and serializes descriptor spans.
src/​protocol_messages.h Defines compact sets and view-backed hello structures.
src/​player_role.cpp Supplies player capabilities through a format span.
src/​platform/​network_info.h Defines the fixed-size MAC text return type.
src/​host/​network_info.cpp Returns detected host MAC addresses in fixed arrays.
src/​esp/​network_info.cpp Returns detected ESP MAC addresses in fixed arrays.
src/​connection.h Stores activation sets and reads their masks directly.
src/​connection_manager.cpp Uses sets for activation handling and builds hello in place.
src/​client.cpp Builds hello views and owns detected-MAC storage in the message.
src/​client_dispatch.cpp Logs activity membership instead of list size.
src/​admission.h Replaces activity-vector scans with set membership checks.
include/​sendspin/​client.h Updates the private in-place hello builder declaration.

Review effort: Balanced

client/hello copied the name, the device info strings, the pairing-method
lists and the player's formats out of the config, and grew its role and
pair-method vectors, for 12 operator new allocations (15 under libstdc++)
on every connection. ClientHelloMessage now views the config instead:
string_views and spans, with its roles and pair methods in InlineVectors,
so it is filled in place and serialized before anything it views can
change. platform_get_interface_mac() returns the MAC as a fixed array,
which the message holds when the MAC is detected rather than configured.

Accept to admission: 44 -> 32 allocations under libc++, 50 -> 35 under
libstdc++.
server/activate parsed active_roles into a vector of role strings and
activities into a vector, and the connection and the manager copied both,
for 8 operator new allocations on every admission. ActivitySet holds the
activities as bits, and ActiveRoles holds the roles as the mask the gates
already read plus whether any role was named, so a version this library
does not implement still makes the set non-empty as admission requires.
role_from_string() replaces role_in() and active_role_mask().

Accept to admission: 32 -> 24 allocations under libc++, 35 -> 27 under
libstdc++.
ServerActivateMessage is trivially copyable now that its activities and
roles are sets, so moving it into on_server_activate() did nothing
(performance-move-const-arg). The handler only reads it, so it takes a
const reference.
@kahrendt
kahrendt merged commit 175c1e1 into main Nov 3, 2026
16 checks passed
@kahrendt
kahrendt deleted the alloc-3-messages branch November 3, 2026 16:01
kahrendt added a commit that referenced this pull request Nov 5, 2026
…llo (#218)

Add `SendspinClientConfig::application_roles` so an application can list
its own `_`-prefixed, versioned roles in `client/hello`
`supported_roles`, after the library's roles. A role can also carry a
support object, which is sent as `"<role>_support"` (messaging.md
"client/hello", README.md "Application-Specific Roles"). The roles
defined in the spec are unchanged.

`start()` refuses to start (it logs an error and returns false) when:
- a role name is not `_<name>@<version>` in visible ASCII;
- a role name is listed twice;
- a support object is not exactly one JSON object.

ArduinoJson's parser accepts input that isn't strict JSON, such as
unquoted keys and single quotes. So instead of embedding the caller's
text, `start()` re-serializes each support object and sends that. A
support object is also refused if a string in it is not valid UTF-8, or
has a control character the serializer would write back unescaped.

Application-specific versions of defined roles (`player@_x`) are out of
scope: the library implements those roles itself.

This is the generic hook that local pairing with Music Assistant needs,
e.g. `"_ma_local_pairing@v1_support": {"port": N}`.

Part of Sendspin/backlog#86.

## Allocation and limit fixes

Merged main twice (#227 onward) and fixed what the allocation series
requires of client/hello (Sendspin/backlog#92):

- The hello views the configured roles (`std::span` over the config
`start()` normalized) instead of copying them per connection, builds
each `"<role>_support"` key on the stack, and embeds the support object
with `serialized(data, size)`, which copies it into the hello's JSON
arena (the platform heap past its budget) rather than through `operator
new`. The admission window of `sendspin_alloc_tests` now configures two
application roles and stays at 0.
- `start()` also refuses a support object nested past messaging.md
"client/hello"'s 10-level cap, and a hello longer than its 16384 bytes,
measured on a trial hello with the per-connection fields at their
longest; that failure rolls back the roles it started
(`ClientLifecycle.OversizedHelloRollsBackStartedRoles`, which fails
without the rollback by finding the player's sync task still bound to
the ring).
- Role names are at most 64 bytes, a library limit, and exclude `"` and
`\`. `is_valid_utf8()` lives in `inline_text.h` with its own table,
which rejects a bad byte at every continuation position.
- The integration guide says the library does not report which
application roles a server activates, and that one listed in
`active_roles` on a connection that is not playback-capable closes it.

---------

Co-authored-by: Kevin Ahrendt <kahrendt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants